package com.poseidon.prototype;


import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

//gestisce i tag del file XML
public class XMLhandler extends DefaultHandler{

     // ===========================================================
     // Fields
     // ===========================================================


    private Physics physics; // = new WhatMustDo();

	private boolean gravity;
	private boolean robotMass;
	private boolean friction;
	private boolean robotMaxSpeed;
	private boolean robotLenght;
	private boolean robotHeight;
	private boolean robotWidth;
	private boolean robotPayload;
	private boolean robotThrustForwardForce;
	private boolean robotThrustLateralForce;
	private boolean robotThrustVerticalForce;
	private boolean waterDensity;
	private boolean maximumWorkingDepth;
	private boolean frictionAreaReference;
	private boolean frictionDragCoefficient;

     // ===========================================================
     // Getter & Setter
     // ===========================================================

     public Physics getParsedData() {

          return this.physics;
     }

     // ===========================================================
     // Methods
     // ===========================================================
     @Override
     public void startDocument() throws SAXException {
          this.physics = new Physics();
     }

     @Override
     public void endDocument() throws SAXException {
          // Nothing to do
     }

     /** Gets be called on opening tags like:
      * <tag>
      * Can provide attribute(s), when xml was like:
      * <tag attribute="attributeValue">*/
     @Override //when the parser reaches an opening-tag the startElement(...)-method gets called:
     		   //So we will simply check the localName and set the corresponding "in_xyz"-boolean to true.
     		   //OGNI VOLTA CHE VEDE UN TAG SI FERMA QUA E VEDE QUALE E'
     		   //facendo dei booleani per ogni tag vedi quelli presenti
     		   // se in un tag c'e informazioni tag>characters</tag> in automatico chiama il metodo characters
     public void startElement(String namespaceURI, String localName,
    		 				  String qName, Attributes atts) throws SAXException {

    	 //If the name of the tag is "gravity"
    	 //means that exist and we have to take care of it
          if (qName.equals("gravity")) {
        	  this.gravity = true;
          }else if (qName.equals("robotMass")){
        	  this.robotMass=true;
          } else if (qName.equals("friction")){
        	  this.friction=true;
          } else if (qName.equals("maximumWorkingDepth")){
        	  this.maximumWorkingDepth=true;
          } else if (qName.equals("robotLenght")){
        	  this.robotLenght=true;
          } else if (qName.equals("robotHeight")){
        	  this.robotHeight=true;
          } else if (qName.equals("robotWidth")){
        	  this.robotWidth=true;
          } else if (qName.equals("robotMaxSpeed")){
        	  this.robotMaxSpeed=true;
          } else if (qName.equals("robotPayload")){
        	  this.robotPayload=true;
          } else if (qName.equals("robotThrustForwardForce")){
        	  this.robotThrustForwardForce=true;
          } else if (qName.equals("robotThrustLateralForce")){
        	  this.robotThrustLateralForce=true;
          } else if (qName.equals("robotThrustVerticalForce")){
        	  this.robotThrustVerticalForce=true;
          } else if (qName.equals("waterDensity")){
        	  this.waterDensity=true;
          } else if (qName.equals("frictionAreaReference")){
        	  this.frictionAreaReference=true;
          } else if (qName.equals("frictionDragCoefficient")){
        	  this.frictionDragCoefficient=true;
          }
     }

     /** Gets be called on closing tags like:
      * </tag> */
     @Override
     public void endElement(String namespaceURI, String localName, String qName)
               throws SAXException {
    	 if (qName.equals("gravity")) {
	       	  this.gravity=false;
         }else if (qName.equals("robotMass")) {
	          this.robotMass=false;
         }else if (qName.equals("friction")) {
	          this.friction=false;
         }else if (qName.equals("maximumWorkingDepth")) {
	          this.maximumWorkingDepth=false;
         }else if (qName.equals("waterDensity")) {
	          this.waterDensity=false;
         }else if (qName.equals("robotThrustVerticalForce")) {
	          this.robotThrustVerticalForce=false;
         }else if (qName.equals("robotThrustLateralForce")) {
	          this.robotThrustLateralForce=false;
         }else if (qName.equals("robotThrustForwardForce")) {
	          this.robotThrustForwardForce=false;
         }else if (qName.equals("robotPayload")) {
	          this.robotPayload=false;
         }else if (qName.equals("robotLenght")) {
	          this.robotLenght=false;
         }else if (qName.equals("robotMaxSpeed")) {
	          this.robotMaxSpeed=false;
         }else if (qName.equals("robotWidth")) {
	          this.robotWidth=false;
         }else if (qName.equals("robotHeight")) {
	          this.robotHeight=false;
         }else if (qName.equals("frictionAreaReference")) {
	          this.frictionAreaReference=false;
         }else if (qName.equals("frictionDragCoefficient")) {
	          this.frictionDragCoefficient=false;
         }
    }


     /** Gets be called on the following structure: insomma quando c'e il tag in mezzo
      * <tag>characters</tag> */
     @Override
    public void characters(char ch[], int start, int length) {
    	 if(this.gravity){
    		 physics.setGravityValue(new String(ch, start, length));
         }else if(this.robotMass){
        	 physics.setRobotMass(new String(ch, start, length));
         }else if(this.friction){
        	 physics.setFriction(new String(ch, start, length));
         }else if(this.robotMaxSpeed){
        	 physics.setRobotMaxSpeed(new String(ch, start, length));
         }else if(this.robotLenght){
        	 physics.setRobotLenght(new String(ch, start, length));
         }else if(this.robotHeight){
        	 physics.setRobotHeight(new String(ch, start, length));
         }else if(this.robotWidth){
        	 physics.setRobotWidth(new String(ch, start, length));
         }else if(this.robotThrustForwardForce){
        	 physics.setRobotThrustForwardForce(new String(ch, start, length));
         }else if(this.robotThrustLateralForce){
        	 physics.setRobotThrustLateralForce(new String(ch, start, length));
         }else if(this.robotThrustVerticalForce){
        	 physics.setRobotThrustVerticalForce(new String(ch, start, length));
         }else if(this.waterDensity){
        	 physics.setWaterDensity(new String(ch, start, length));
         }else if(this.maximumWorkingDepth){
        	 physics.setMaximumWorkingDepth(new String(ch, start, length));
         }else if(this.robotPayload){
        	 physics.setRobotPayload(new String(ch, start, length));
         }else if(this.frictionAreaReference){
        	 physics.setFrictionAreaReference(new String(ch, start, length));
         }else if(this.frictionDragCoefficient){
        	 physics.setFrictionDragCoefficient(new String(ch, start, length));
         }
     }
}